<template>
  <!-- 报警管理 -->
  <div style="margin-top: 15px;">
    <!-- ## 条件搜索 -->
    <el-row style="background-color: #FFFFFF;margin-top: 10px;">
      <el-form :inline="true" :rules="rules" :model="searchData" class="demo-form-inline" style="margin-left: 25px;">
        <el-form-item label="主机名">
          <el-input v-model="searchData.hostName" placeholder="主机名" />
        </el-form-item>
        <el-form-item label="收件人邮箱">
          <el-input v-model="searchData.toEmail" placeholder="收件人邮箱" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
        </el-form-item>
        <el-form-item style="margin:0 30px;">
          <el-button type="primary" icon="el-icon-refresh" circle @click="handleRefresh" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="addDuty">添加邮件</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <!-- # 表格数据 -->
    <el-table v-loading="loading" element-loading-text="正在加载中" :data="tableData" style="width: 99%">
      <el-table-column width="60" type="index" label="序号" />

      <el-table-column label="主机" width="150" align="center">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>主机名: {{ scope.row.hostName }}</p>
            <p>端口: {{ scope.row.smtpPort }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.hostName }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>

      <el-table-column label="端口" width="180" align="center">
        <template slot-scope="scope">
          <el-tag size="medium" type="success" effect="plain">{{ scope.row.smtpPort }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="收件人邮箱" width="180" prop="toEmail" />
      <el-table-column label="发件人人邮箱" width="240" prop="fromMailName" />

      <el-table-column label="邮箱代码" width="180" prop="fromPwd" />

      <el-table-column label="SMTP" width="180" prop="smtpHost" />

      <el-table-column label="操作" align="center" min-width="150" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-permission="'duty:modify'"
            size="mini"
            type="info"
            @click="handleEdit(scope.row)"
          >编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
      <template v-slot:empty>
        <div style="height: 200px;">
          <img src="../../assets/nodata.png" width="150" style="margin: 30px;">
        </div>
      </template>
    </el-table>
    <!-- # 分页 -->
    <!-- 分页 -->
    <el-pagination
      :page-size="size"
      :current-page.sync="page"
      :page-sizes="[10, 15, 20, 25, 100]"
      layout="sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- # 弹层表单 -->
    <el-dialog :title="isEdit ? '编辑' : '添加'" :visible.sync="dialogVisible" width="50%">
      <div>
        <el-form ref="formDateRef" :rules="rules" label-position="right" label-width="100px" :model="formDate">

          <el-form-item label="收件人邮箱" prop="toEmail">
            <el-input v-model="formDate.toEmail" />
          </el-form-item>

          <el-form-item label="发件人邮箱" prop="fromMailName">
            <el-input v-model="formDate.fromMailName" />
          </el-form-item>

          <el-form-item label="发件人密码" prop="fromPwd">
            <el-input v-model="formDate.fromPwd" placeholder="请输入密码" show-password />
          </el-form-item>

          <el-form-item label="主机名" prop="hostName">
            <el-input v-model="formDate.hostName" />
          </el-form-item>

          <el-form-item label="SMTP服务器" prop="smtpHost">
            <el-input v-model="formDate.smtpHost" />
          </el-form-item>

          <el-form-item label="服务器端口" prop="smtpPort">
            <el-input v-model="formDate.smtpPort" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelBtn">取 消</el-button>
        <el-button type="primary" @click="verBtn">确 定</el-button>
      </span>
    </el-dialog>

  </div>

</template>

<script>
import { getEmail, addEmail, changeEmail, deleteEmail } from '@/api/email.js'
export default {
  data() {
    return {
      dialogVisible: false, // 弹层是否开启
      isEdit: true, // 是否是编辑
      currentPage3: 1, // 当前页数
      tableData: [], // 表格数据
      searchData: {
        hostName: '',
        toEmail: ''
      },
      formDate: {
        id: '', // ID
        toEmail: '', // 收件人邮箱
        fromMailName: '', // 发件人邮箱
        fromPwd: '', // 发件人密码
        smtpHost: '', // SMTP服务器
        smtpPort: '', // 服务器端口
        hostName: '', // 主机名
        from: ''
      },
      rules: {
        toEmail: [{ required: true, type: 'email', message: '请输入收件人邮箱', trigger: 'blur' }],
        fromMailName: [{ required: true, type: 'email', message: '请输入发件人邮箱', trigger: 'blur' }],
        fromPwd: [{ required: true, message: '发件人密码', trigger: 'blur' }],
        hostName: [{ required: true, trigger: 'blur' }],
        smtpHost: [{ required: true, validator: this.validateIP, trigger: 'blur' }],
        smtpPort: [{ required: true, message: '请输入服务器端口', trigger: 'blur' }]

      },
      total: 0,
      size: 10,
      page: 1,
      loading: true
    }
  },
  mounted() {
    this.getEmail()
  },
  methods: {
    async handleRefresh() {
      this.loading = true
      this.searchData.hostName = ''
      this.searchData.toEmail = ''
      await this.getEmail()
      this.loading = false
      this.size = 10
      this.page = 1
    },
    commonHandle(res) {
      this.tableData = res.records
      this.total = res.total
      this.size = res.size
      this.loading = false
    },
    // 获取表格数据
    async getEmail() {
      if (this.searchData.hostName === '' && this.searchData.toEmail === '') {
        const res = await getEmail(this.page, this.size)
        this.commonHandle(res)
      } else if (this.searchData.hostName === '') {
        const reqData = {
          toEmail: this.searchData.toEmail
        }
        const res = await getEmail(this.page, this.size, reqData)
        this.commonHandle(res)
      } else if (this.searchData.toEmail === '') {
        const reqData = {
          hostName: this.searchData.hostName
        }
        const res = await getEmail(this.page, this.size, reqData)
        this.commonHandle(res)
      } else {
        const res = await getEmail(this.page, this.size, this.searchData)
        this.commonHandle(res)
      }
      this.loading = false
    },
    // 新增
    addDuty() {
      this.formDate = {
        id: '',
        toEmail: '',
        fromMailName: '',
        fromPwd: '',
        smtpHost: '',
        smtpPort: '',
        hostName: '',
        from: ''
      }
      this.dialogVisible = true
      this.isEdit = false
    },
    // 编辑
    handleEdit(row) {
      console.log('当前行数据：', row)
      this.dialogVisible = true
      this.isEdit = true
      this.formDate = {
        id: row.id,
        toEmail: row.toEmail,
        fromMailName: row.fromMailName,
        fromPwd: row.fromPwd,
        smtpHost: row.smtpHost,
        smtpPort: row.smtpPort,
        hostname: row.hostName
      }
      // console.log(this.formDate)
    },
    // 删除
    handleDelete(row) {
      console.log('id:', row.id)
      this.$confirm('此操作将删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteEmail(row.id)
        await this.getEmail()
      })
    },
    // 弹层 取消
    cancelBtn() {
      this.dialogVisible = false
    },
    // 添加/编辑 确认按钮
    async verBtn() {
      // 编辑-确认
      if (this.isEdit) {
        this.$refs.formDateRef.validate(async isok => {
          if (isok) {
            // 编辑接口
            // console.log(this.formDate)
            // 编辑接口
            await changeEmail(this.formDate)
            this.$message({
              message: '编辑成功！',
              type: 'success'
            })
            this.dialogVisible = false
          }
        })
      // 添加-确认
      } else {
        console.log('确认')
        this.$refs.formDateRef.validate(async isok => {
          if (isok) {
            console.log(this.formDate)
            // 添加接口
            await addEmail(this.formDate)
            await this.getEmail()
            this.$message({
              message: '添加成功！',
              type: 'success'
            })
            this.dialogVisible = false
          }
        })
      }
    },
    handleSizeChange(size) {
      this.loading = true
      this.size = size
      this.page = 1
      this.getEmail()
    },
    // 分页-page
    handleCurrentChange(page) {
      this.loading = true
      this.page = page
      this.getEmail()
    },
    validateIP(rule, value, callback) {
      if (value === '') {
        callback(new Error('请输入服务器IP地址'))
      } else {
        const ipPattern = /^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$/
        if (!ipPattern.test(value)) {
          callback(new Error('请输入有效的IP地址'))
        } else {
          callback()
        }
      }
    },
    // 条件查询
    async handleSearch() {
      this.loading = true
      await this.getEmail()
    }
  }
}
</script>

<style lang="scss" scoped></style>
